package com.training.net;

import android.util.Log;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okio.Buffer;

public class LoggingInterceptor implements Interceptor {
    private static final String TAG = "NetworkLog";
    
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        
        // 打印请求信息
        logRequest(request);
        
        // 执行请求
        long startTime = System.currentTimeMillis();
        Response response = chain.proceed(request);
        long endTime = System.currentTimeMillis();
        
        // 打印响应信息
        logResponse(response, endTime - startTime);
        
        return response;
    }
    
    private void logRequest(Request request) {
        StringBuilder sb = new StringBuilder();
        sb.append("\n==================== REQUEST ====================\n");
        sb.append("Method: ").append(request.method()).append("\n");
        sb.append("URL: ").append(request.url()).append("\n");
        
        // 打印请求头
        sb.append("Headers:\n");
        for (int i = 0, size = request.headers().size(); i < size; i++) {
            sb.append("  ").append(request.headers().name(i))
              .append(": ").append(request.headers().value(i)).append("\n");
        }
        
        // 打印请求体
        if (request.body() != null) {
            Buffer buffer = new Buffer();
            try {
                request.body().writeTo(buffer);
                String requestBody = buffer.readUtf8();
                sb.append("Body: ").append(requestBody).append("\n");
            } catch (IOException e) {
                sb.append("Body: Could not read request body\n");
            }
        }
        
        sb.append("==================================================\n");
        Log.d(TAG, sb.toString());
    }
    
    private void logResponse(Response response, long duration) {
        StringBuilder sb = new StringBuilder();
        sb.append("\n==================== RESPONSE ====================\n");
        sb.append("URL: ").append(response.request().url()).append("\n");
        sb.append("Status Code: ").append(response.code()).append("\n");
        sb.append("Time: ").append(duration).append("ms\n");
        
        // 打印响应头
        sb.append("Headers:\n");
        for (int i = 0, size = response.headers().size(); i < size; i++) {
            sb.append("  ").append(response.headers().name(i))
              .append(": ").append(response.headers().value(i)).append("\n");
        }
        
        // 打印响应体
        try {
            String responseBody = response.peekBody(1024 * 1024).string();
            sb.append("Body: ").append(responseBody).append("\n");
        } catch (IOException e) {
            sb.append("Body: Could not read response body\n");
        }
        
        sb.append("==================================================\n");
        Log.d(TAG, sb.toString());
    }
}
